<?php


/**
 * Notes: 取消订单
 * PROJECT_NAME : ERP v1.8 重构erp
 * User: 夏鱼儿<397072174@qq.com>
 * Date: 2024/5/17
 * Time: 11:23
 */

namespace app\service\order;

use Exception;
use think\facade\Log;

class Cancel extends Base
{

    /**
     * 取消订单
     * @return void
     * @throws Exception
     */
    public function update(): void
    {
        $key        = kcGoodsRuleKey() . $this->order->company_id;
        $goodsKey   = kcGoodsKey() . $this->order->company_id;
        $xsKey      = xsGoodsRuleKey() . $this->order->company_id;
        $xsGoodsKey = xsGoodsKey() . $this->order->company_id;
        redis()->multi();//开启事务
        try {
            $this->order->storeOrderDetails()->select()->each(function ($item) use ($key, $xsKey, $goodsKey, $xsGoodsKey) {
                redis()->zIncrBy($key, $item['quantity'], $item['goods_rule_id']);//库存+
                redis()->zIncrBy($goodsKey, $item['quantity'], $item['goods_id']);//库存+
                redis()->zIncrBy($xsKey, $item['quantity'] * -1, $item['goods_rule_id']);//销量-
                redis()->zIncrBy($xsGoodsKey, $item['quantity'] * -1, $item['goods_id']);//销量-
            });
            redis()->exec();//提交事务
        } catch (Exception $e) {
            redis()->discard();//回滚
            exception($e->getMessage());
        }
    }
}